//
// Created by ypfan on 2020/12/31.
//

#include "mysqlConn.h"
#include "mysql/mysql.h"

MYSQL mysql;
MYSQL_RES* res;
MYSQL_ROW row;
unsigned int num_fields;

bool ConnectDatabase()
{
    mysql_init(&mysql);
    if((mysql_real_connect(&mysql,"127.0.0.1","root","DolphinDB123","mysql",3306,nullptr,0))){
        printf("Error connecting to database:%s\n",mysql_error(&mysql));
        return false;
    }
    else{
        printf("connected...\n");
        return true;
    }
}

void QueryData(std::string& query_sql_string)
{
    const char* query_buffer=query_sql_string.c_str();

    if(mysql_query(&mysql,query_buffer))
        printf("Query failed(%s)\n",mysql_error(&mysql));
    else
    {
        printf("Query success\n");

        res=mysql_store_result(&mysql);
        num_fields=mysql_num_fields(res);
        while((row=mysql_fetch_row(res))!=nullptr)
            if(row[6]!=nullptr)
                cout << row[6] << endl;
        mysql_free_result(res);    //释放内存
    }
}

slave_test::slave_test() {
    cout << "hello master" << endl;
    ConnectDatabase();
    std::string query_string = R"(select * from mysql.user;)";
    QueryData(query_string);
    mysql_close(&mysql);
}
